package com.teacold.customdemo.chapter_three;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.teacold.customdemo.R;

import androidx.appcompat.app.AppCompatActivity;

public class Property_ValueAnimator extends AppCompatActivity implements View.OnClickListener {
    private TextView value_text;
    private Button value_button;
    private ValueAnimator valueAnimator;
    private ImageView value_oval;
    private FallingBallImageView value_oval2;
    private ObjectAnimator objectAnimator;
    private AnimatorSet animatorSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.property__valueanimator_layout);
        initView();
        initMonitor();
    }

    private void initView() {
        value_text = findViewById(R.id.value_text);
        value_button = findViewById(R.id.value_button);
        value_oval=findViewById(R.id.value_oval);
        value_oval2=findViewById(R.id.value_oval2);


        animatorBuilder();
//        animatorSet();
//           customAnimator();
//           objectAnimatorDemo();
//         fallingBallDemo();
//        valueAnimatorEvaluator();
//      valueAnimatorDemo();//简单的使用与监听

    }

    private void animatorBuilder() {
        ObjectAnimator tvBgAnimator=ObjectAnimator.ofInt(value_text,"BackgroundColor",0xffff00ff,0xffffff00,0xffff00ff);
        ObjectAnimator tvTranskateY=ObjectAnimator.ofFloat(value_text,"translationY",0,300,0);
        ObjectAnimator tvTranskateY2=ObjectAnimator.ofFloat(value_oval,"translationY",0,400,0);
        animatorSet=new AnimatorSet();
        AnimatorSet.Builder builder=animatorSet.play(tvTranskateY2);
        builder.with(tvBgAnimator);
//        builder.after(tvTranskateY);
//        animatorSet.start();
    }

    private void animatorSet() {
        ObjectAnimator tvBgAnimator=ObjectAnimator.ofInt(value_text,"BackgroundColor",0xffff00ff,0xffffff00,0xffff00ff);
        ObjectAnimator tvTranskateY=ObjectAnimator.ofFloat(value_text,"translationY",0,300,0);
        ObjectAnimator tvTranskateY2=ObjectAnimator.ofFloat(value_oval,"translationY",0,400,0);
        animatorSet=new AnimatorSet();
        animatorSet.playTogether(tvBgAnimator,tvTranskateY,tvTranskateY2);
        animatorSet.setDuration(1000);
    }

    private void customAnimator() {
       objectAnimator=ObjectAnimator.ofObject(value_oval2,"fallingPos",new FallingBallEvaluator(),new Point(0,0),new Point(500,500));
       objectAnimator.setDuration(5000);
    }

    private void objectAnimatorDemo() {
     objectAnimator=ObjectAnimator.ofFloat(value_text,"rotationY",0,270,0);
     objectAnimator.setDuration(2000);

    }

    private void fallingBallDemo() {
        valueAnimator=ValueAnimator.ofObject(new FallingBallEvaluator(),new Point(0,0),new Point(500,500));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
               Point mCurPoint= (Point) animation.getAnimatedValue();
               value_oval.layout(mCurPoint.x,mCurPoint.y,mCurPoint.x+value_oval.getWidth(),mCurPoint.y+value_oval.getHeight());
            }
        });
        valueAnimator.setDuration(2000);
    }

    private void valueAnimatorEvaluator() {
        valueAnimator=ValueAnimator.ofObject(new CharEvaluator(),new Character('A'),new Character('Z'));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                char text= (char) animation.getAnimatedValue();
                value_text.setText(String.valueOf(text));
            }
        });
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    }

    private void valueAnimatorDemo() {
        valueAnimator = ValueAnimator.ofInt(0, 400,600,100);
        valueAnimator.setDuration(700);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int curVaiue = (int) animation.getAnimatedValue();
                animation.setRepeatCount(ValueAnimator.INFINITE);
                value_text.layout(curVaiue, curVaiue, curVaiue + value_text.getWidth(), curVaiue + value_text.getHeight());
            }
        });
        valueAnimator.setDuration(700);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
    }

    private void initMonitor() {
        value_text.setOnClickListener(this);
        value_button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.value_text:
                Toast.makeText(this, "农妇山泉有点田", Toast.LENGTH_LONG).show();
                break;
            case R.id.value_button:
                if(valueAnimator!=null){
                    valueAnimator.start();
                }
                if(objectAnimator!=null){
                    objectAnimator.start();
                }
                if(animatorSet!=null){
                    animatorSet.start();
                }
                break;

        }
    }
}
